package cn.seqdata.cxf.wrapper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;

import javax.validation.ConstraintViolation;
import javax.validation.ElementKind;
import javax.validation.Path;

public class WrapperViolations extends WrapperBean {
	private static final long serialVersionUID = 1L;

	/** 验证的输入字段问题 */
	private final Map<String, String> violations = new HashMap<>();

	public WrapperViolations(Set<ConstraintViolation<?>> violations) {
		super();

		setViolations(violations);
	}

	@Override
	public boolean isSuccess() {
		return false;
	}

	public Map<String, String> getViolations() {
		return violations;
	}

	private void setViolations(Set<ConstraintViolation<?>> violations) {
		for (ConstraintViolation<?> violation : violations) {
			String path = getKindPath(violation.getPropertyPath(), ElementKind.PROPERTY);
			if (null != path) {
				this.violations.put(path, violation.getMessage());
			}
		}
	}

	private String getKindPath(Path path, final ElementKind kind) {
		StringJoiner joiner = new StringJoiner(",");

		path.forEach(x -> {
			if (x.getKind() == kind) {
				joiner.add(x.getName());
			}
		});

		return joiner.toString();
	}
}
